package hc.bre

import static org.junit.jupiter.api.Assertions.*

import hc.bre.clas.ClasRuleDefinition
import hc.bre.clas.range.RangeDefinition
import hc.bre.clas.yesno.YesNoDefinition
import hc.bre.cmn.IDetailList
import hc.bre.cmn.ListOption
import hc.bre.cmn.RuleDefinition
import hc.bre.cmn.TreeOption
import hc.bre.cmn.struct.DataStruct

import org.junit.jupiter.api.Test

@groovy.transform.CompileStatic
class BasicSomethingTest {

	@Test
	void esc() {
		assert hc.Util.esc('abc') == 'abc'
		assert hc.Util.esc('a"bc') == 'a\\"bc'
		assert hc.Util.esc('a"bcde"') == 'a\\"bcde\\"'
		assert hc.Util.esc("'") == "\\'"
		assert hc.Util.esc("'").length()  == 2;
	}

	@Test
	void listOption() {
		def set = new IDetailList<ListOption>() {
					List<ListOption> list = [new ListOption(id:1,code:'c1',name:'n1'), new ListOption(id:2,code:'c2',name:'n2')];
				};

		ListOption opt;
		opt = set.find(1);
		assert opt.code =='c1';

		opt = set.find(3);
		assertNull opt;

		assert set.routeOf(2)[0].code == 'c2'
		assert set.routeOf(3).size() ==  0;
	}

	@Test
	void treeOption() {
		def set = new TreeOption(id:1,code:'c1',name:'n1',
		list:[new TreeOption(id:4,code:'c4',name:'n4'), new TreeOption(id:2,code:'c2',name:'n2')]) ;

		TreeOption opt;
		opt = set.find(1);
		assert opt.code == 'c1'
		opt = set.find(2);
		assert opt.code == 'c2'
		assertNull set.find(3);

		List<TreeOption> lst = set.routeOf(1)
		assert lst.size() == 1
		assert lst[0].code == 'c1'

		lst = set.routeOf(2)
		assert lst.size() == 2
		assert lst[0].code == 'c1'
		assert lst[1].code == 'c2'

		lst = set.routeOf(3)
		assert lst.size() == 0
	}

	@Test
	void listTreeOption() {
		def set = new IDetailList<TreeOption>() {
					List<TreeOption> list = [
						new TreeOption(id:1,code:'c1',name:'n1',
						list:[
							new TreeOption(id:4,code:'c4',name:'n4',
							list:[new TreeOption(id:41,code:'c41',name:'n41')
								, new TreeOption(id:42,code:'c42',name:'n42')])
							,
							new TreeOption(id:2,code:'c2',name:'n2')
						]),
						new TreeOption(id:3,code:'c3',name:'n3')
					];
				};


		TreeOption opt;
		opt = set.find(1);
		assert opt.code == 'c1'
		opt = set.find(2);
		assert opt.code == 'c2'
		opt = set.find(41);
		assert opt.code == 'c41'

		assertNull set.find(5);

		List<TreeOption> lst = set.routeOf(1)
		assert lst.size() == 1
		assert lst[0].code == 'c1'

		lst = set.routeOf(2)
		assert lst.size() == 2
		assert lst[0].code == 'c1'
		assert lst[1].code == 'c2'

		lst = set.routeOf(41)
		assert lst.size() == 3
		assert lst[0].code == 'c1'
		assert lst[1].code == 'c4'
		assert lst[2].code == 'c41'

		lst = set.routeOf(5)
		assert lst.size() == 0
	}
}
